﻿using System;
using Eve.TapToClick.Models;
using Eve.TapToClick.NativeInterop;

namespace Eve.TapToClick.Core.Control
{
    abstract class MainControl
    {
        protected AppConfiguration appConfiguration;
        // 鼠标按钮
        protected MouseButtonCase mouseButtonCase;
        // 控制器位置(1~5)
        protected int controlLocal;
        // 触发点true为触发，fakse为未触发
        public bool ActiveContacts { get; set; }
        // 手指接触触碰板的大小
        public double ContactDistances { get; set; }
        // 手指按压使用的压力值，第一次按下的压力，如果后续有更大的压力将会被覆盖
        public uint PressureBeg { get; set; }
        // 手指按压使用的压力值，第一次之后的，随时都会变动
        public uint PressureWait{ get; set; }
        // 按下时间
        public DateTime DownDateTime { get; set; }
        // 松开时间，如果等于按下时间,则当前值不合法
        public DateTime UpDateTime { get; set; }
        // X轴
        protected uint[] XValues = new uint[] { 0, 0 };
        // Y轴
        protected uint[] YValues = new uint[] { 0, 0 };
        // 添加坐标数量
        private int addPosNum = 0;

        public MainControl(AppConfiguration appConfiguration, MouseButtonCase mouseButtonCase, int controlLocal)
        {
            this.appConfiguration = appConfiguration;
            this.mouseButtonCase = mouseButtonCase;
            this.controlLocal = controlLocal;
        }

        public void Clean()
        {
            if (addPosNum > 0) {
                ActiveContacts = false;
                ContactDistances = 0;
                PressureBeg = 0;
                PressureWait = 0;
                DownDateTime = DateTime.Now;
                UpDateTime = DownDateTime;
                XValues = new uint[] { 0, 0 };
                YValues = new uint[] { 0, 0 };
                addPosNum = 0;
            }
        }

        public void addPos(uint x, uint y, double ContactDistances)
        {
            if (addPosNum == 0)
            {
                XValues[0] = x;
                YValues[0] = y;
            } else {
                XValues[0] = XValues[1];
                YValues[0] = YValues[1];
                XValues[1] = x;
                YValues[1] = y;
            }
            this.ContactDistances = ContactDistances;
            addPosNum ++;
        }

        public void setPressureBeg(uint PressureBeg)
        {
            this.PressureBeg = PressureBeg;
        }

        public void setPressureWait(uint PressureWait)
        {
            this.PressureWait = PressureWait;
            if(PressureWait > PressureBeg)
            {
                PressureBeg = PressureWait;
            }
        }

        public void setFirst(bool ActiveContacts, DateTime DownDateTime)
        {
            this.ActiveContacts = ActiveContacts;
            this.DownDateTime = DownDateTime;
        }

        public void setLast(bool ActiveContacts, DateTime UpDateTime)
        {
            this.ActiveContacts = ActiveContacts;
            this.UpDateTime = UpDateTime;
        }

        /// <summary>
        /// 单次点击是否通过
        /// </summary>
        /// <returns>true通过,false不通过</returns>
        public bool SingleClick()
        {
            DateTime tapEnd = DateTime.Now;
            if ((tapEnd - DownDateTime).TotalMilliseconds <= appConfiguration.MaxTapMilliseconds &&
                ContactDistances <= appConfiguration.MaxTapDeltaPosition &&
                PressureBeg >= appConfiguration.TapTriggerThreshold)
            {
                return true;
            }
            return false;
        }

        public abstract void First();

        public abstract void Sustained();

        public abstract void Last();

    }
}
